using System.Web.Mvc;
using ETestIT.Core;
using SharpArch.Core.PersistenceSupport;
using System.Collections.Generic;
using SharpArch.Web.NHibernate;
using SharpArch.Core;

namespace ETestIT.Web.Controllers
{
    using Extensions;

    [HandleError]
    public class ResumesController : Controller
    {
        public ResumesController(IRepository<Resume> resumeRepository) {
            Check.Require(resumeRepository != null, "resumeRepository may not be null");

            this.resumeRepository = resumeRepository;
        }

        [Transaction]
        public ActionResult Index() {
            IList<Resume> resumes = resumeRepository.GetAll();
            return View(resumes);
        }

        public ActionResult Create() {
            ResumeFormViewModel viewModel = ResumeFormViewModel.CreateResumeFormViewModel();
            return View(viewModel);
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Create(Resume resume) {

            this.GetResumeFromRequest(resume);

            if (resume.IsValid()) {
                resumeRepository.SaveOrUpdate(resume);

                TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = 
					"The resume was successfully created.";
                return RedirectToAction("Index");
            }

            ResumeFormViewModel viewModel = ResumeFormViewModel.CreateResumeFormViewModel();
            viewModel.Resume = resume;
            return View(viewModel);
        }

        [Transaction]
        public ActionResult Download(int id)
        {
            return new ResumeActionResult(resumeRepository.Get(id));
        }

        [ValidateAntiForgeryToken]
        [Transaction]
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult Delete(int id) {
            string resultMessage = "The resume was successfully deleted.";
            Resume resumeToDelete = resumeRepository.Get(id);

            if (resumeToDelete != null) {
                resumeRepository.Delete(resumeToDelete);

                try {
                    resumeRepository.DbContext.CommitChanges();
                }
                catch {
                    resultMessage = "A problem was encountered preventing the resume from being deleted. " +
						"Another item likely depends on this resume.";
                    resumeRepository.DbContext.RollbackTransaction();
                }
            }
            else {
                resultMessage = "The resume could not be found for deletion. It may already have been deleted.";
            }

            TempData[ControllerEnums.GlobalViewDataProperty.PageMessage.ToString()] = resultMessage;
            return RedirectToAction("Index");
        }

		/// <summary>
		/// Holds data to be passed to the Resume form for creates and edits
		/// </summary>
        public class ResumeFormViewModel
        {
            private ResumeFormViewModel() { }

			/// <summary>
			/// Creation method for creating the view model. Services may be passed to the creation 
			/// method to instantiate items such as lists for drop down boxes.
			/// </summary>
            public static ResumeFormViewModel CreateResumeFormViewModel() {
                ResumeFormViewModel viewModel = new ResumeFormViewModel();
                
                return viewModel;
            }

            public Resume Resume { get; internal set; }
        }

        private readonly IRepository<Resume> resumeRepository;
    }
}
